﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using Enferno.Public.Web.Models;
using Enferno.StormApiClient.Shopping;

namespace Enferno.Public.Web.Mappers.Resolvers
{
    public class BasketItemsResolver<TBasketModel, TBasketItemModel, TPromotionModel, TOnHandStatusModel, TPriceModel, TProductFileModel> : ValueResolver<TBasketModel, BasketItemList>
        where TBasketModel : BasketBaseModel<TBasketItemModel, TPromotionModel, TOnHandStatusModel, TPriceModel, TProductFileModel>
        where TBasketItemModel : BasketItemBaseModel<TPriceModel>
        where TPromotionModel : PromotionBaseModel<TProductFileModel>
        where TOnHandStatusModel : OnHandStatusModel
        where TPriceModel : PriceModel
        where TProductFileModel : ProductFileModel
    {
        protected override BasketItemList ResolveCore(TBasketModel source)
        {
            var basketItemList = new BasketItemList();
            source.Items.ForEach(item => basketItemList.Add(Mapper.Map<BasketItem>(item)));
            source.Freights.ForEach(item => basketItemList.Add(Mapper.Map<BasketItem>(item)));
            source.Payments.ForEach(item => basketItemList.Add(Mapper.Map<BasketItem>(item)));

            return basketItemList;
        }
    }

    public class BasketModelItemsResolver<TBasketItemModel, TPriceModel> : ValueResolver<Basket, IEnumerable<TBasketItemModel>>
        where TBasketItemModel : BasketItemBaseModel<TPriceModel>
        where TPriceModel : PriceModel
    {
        protected override IEnumerable<TBasketItemModel> ResolveCore(Basket source)
        {
            var items = source.Items.Where(item => item.Type != (int)ProductType.Freight && item.Type != (int)ProductType.Invoice).ToList();

            var basketItemList = items.Select(Mapper.Map<BasketItem, TBasketItemModel>).ToList();

            return basketItemList;
        }
    }

    public class BasketModelPaymentsResolver<TBasketItemModel, TPriceModel> : ValueResolver<Basket, IEnumerable<TBasketItemModel>>
        where TBasketItemModel : BasketItemBaseModel<TPriceModel>
        where TPriceModel : PriceModel
    {
        protected override IEnumerable<TBasketItemModel> ResolveCore(Basket source)
        {
            var items = source.Items.Where(item => item.Type == (int)ProductType.Invoice).ToList();

            var basketItemList = items.Select(Mapper.Map<BasketItem, TBasketItemModel>).ToList();

            return basketItemList;
        }
    }

    public class BasketModelFreightsResolver<TBasketItemModel, TPriceModel> : ValueResolver<Basket, IEnumerable<TBasketItemModel>>
        where TBasketItemModel : BasketItemBaseModel<TPriceModel>
        where TPriceModel : PriceModel
    {
        protected override IEnumerable<TBasketItemModel> ResolveCore(Basket source)
        {
            var items = source.Items.Where(item => item.Type == (int)ProductType.Freight).ToList();

            var basketItemList = items.Select(Mapper.Map<BasketItem, TBasketItemModel>).ToList();

            return basketItemList;
        }
    }
}
